from selenium import webdriver
import unittest
import time
import os
class Baidu1(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.baidu.com/"
        # 脚本运行时，错误的信息将被打印到这个列表中
        self.verificationErrors = []
        # 是否继续接受下一下警告
        self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)
    # 忽略此测试用例
   #  @unittest.skip("skipping")
    def test_baiduSearch(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.maximize_window()
        driver.find_element_by_id("kw").send_keys("刘聪")
        driver.find_element_by_id("su").click()
        time.sleep(3)
        print(driver.title)
        try:
            self.assertEqual(driver.title, "百度一下，你就知道" , msg="不相等")
        except:
            self.saveScreenPhoto(driver, "hao.png")
        time.sleep(3)

    # 截图函数
    def saveScreenPhoto(self,driver,file_name):
        if not os.path.exists("./image"):
            os.makedirs("./image")
        # 截图命名为当时格式化的时间
        now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
        # 把截图命名为文件，拼接上时间和上面弄好的后缀
        driver.get_screenshot_as_file("./image/"+now+"-"+file_name)
        time.sleep(3)

    if __name__ == "__main__":
        unittest.main()
